/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package register;

import exception.RepositoryException;
import base.Task;
import exception.TaskExistsException;
import exception.TaskNotFoundException;
import data.ITaskRepository;
import data.TaskRepositoryXml;
import java.net.URL;
import java.util.ArrayList;

/**
 *
 * @author tacio
 */
public class TaskRegister {

    private ITaskRepository repository;

    public TaskRegister() throws RepositoryException{
        this.repository = new TaskRepositoryXml();
    }

    public void add(Task task) throws RepositoryException, TaskExistsException{
        if (repository.search(task.getName()) == null) {
            repository.add(task);
        } else {
            throw new TaskExistsException(task.getName());
        }
    }

    public void remove(String taskName) throws RepositoryException, TaskNotFoundException{
        if (repository.search(taskName) != null) {
            repository.remove(taskName);
        } else {
            throw new TaskNotFoundException(taskName);
        }
    }

    public void update(Task task) throws RepositoryException, TaskNotFoundException{
        if (repository.search(task.getName()) != null) {
            repository.update(task);
        } else {
            throw new TaskNotFoundException(task.getName());
        }
    }

    public Task search(String taskName) throws RepositoryException{
        return repository.search(taskName);
    }

     public ArrayList<String[]> getTaskList() throws RepositoryException{
        return repository.getTaskList();
    }

     public boolean downloadExists(Task task, URL torrentURL) throws RepositoryException {
         return repository.downloadExists(task, torrentURL);
     }

      public ArrayList<String[]> getDownloadList() throws RepositoryException{
        return repository.getDownloadList();
    }

}
